package com.kgc.springboot.hangduo.service.impl;

import com.kgc.springboot.hangduo.mapper.LawsMapper;
import com.kgc.springboot.hangduo.pojo.Laws;
import com.kgc.springboot.hangduo.pojo.LawsExample;
import com.kgc.springboot.hangduo.service.LawsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: ASUS
 * @date: 2020/12/26 20:38
 * @description:
 */
@Service
public class LawsServiceImpl implements LawsService {
    @Autowired
    LawsMapper lawsMapper;
    @Override
    public List<Laws> selectAll() {
        return lawsMapper.selectByExample(null);
    }


    @Override
    public List<Laws> selectLaws(String alias, String title) {
        LawsExample lawsExample = new LawsExample();
        LawsExample.Criteria criteria = lawsExample.createCriteria();
        List<Laws> list= new ArrayList<>();
        if (alias!=null){
            criteria.andLawsAliasLike("%"+alias+"%");
        }
        if (title!=null){
            criteria.andLawsNameLike("%"+title+"%");
        }

        if (alias==null&&title==null){
            list=lawsMapper.selectByExample(null);
        }else{
            list=lawsMapper.selectByExample(lawsExample);
        }
        return list;
    }

    @Override
    public int addLaws(Laws laws) {
        int i = lawsMapper.insertSelective(laws);
        return i;
    }

    @Override
    public int deleteLaws(int id) {
        int i = lawsMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public int updateLaws(Laws laws) {
        int i = lawsMapper.updateByPrimaryKeySelective(laws);
        return i;
    }

    @Override
    public Laws selectLawsById(int id) {
        Laws laws = lawsMapper.selectByPrimaryKey(id);
        return laws;
    }
}
